#include <iostream>
#include <algorithm>
#include <vector>
#include <algorithm>
#include "Pieces.h"

Pieces::~Pieces(){
	std::for_each(piecesMap.begin(), piecesMap.end(), second_deleter());
}
Pieces::Pieces(Board* board, char * action) {
	this->board = board;
	bRookCount = 0 ;
	bBishopCount = 0 ;
	bKnightCount = 0 ;
	bPawnCount = 0 ;
	wRookCount = 0 ;
	wBishopCount = 0 ;
	wKnightCount = 0 ;
	wPawnCount = 0 ;

}

Pieces::Pieces(Board* board) {
	this->board = board;
	//piecesMap["blackKing"] = new BlackKing(board->GetCell( 2,5 ));
	//piecesMap["whiteKing"] = new WhiteKing(board->GetCell( 0,5 ));
	//piecesMap["blackBishop0"] = new BlackBishop(board->GetCell( 1,5 ), 0);


	piecesMap["whiteKing"] = new WhiteKing(board->GetCell( 7,4 ));
	piecesMap["blackKing"] = new BlackKing(board->GetCell( 0,4 ));
	
	
	
	piecesMap["whiteQueen"] = new WhiteQueen(board->GetCell( 7,3 ));
	piecesMap["blackQueen"] = new BlackQueen(board->GetCell( 0,3 ));
	
	piecesMap["whiteRook0"] = new WhiteRook(board->GetCell( 7,0 ), 0);
	piecesMap["whiteRook1"] = new WhiteRook(board->GetCell( 7,7 ), 1);
	piecesMap["blackRook0"] = new BlackRook(board->GetCell( 0,7 ), 0);
	piecesMap["blackRook1"] = new BlackRook(board->GetCell( 0,0 ), 1);
	
	piecesMap["whiteKnight0"] = new WhiteKnight(board->GetCell( 7,1 ), 0);
	piecesMap["whiteKnight1"] = new WhiteKnight(board->GetCell( 7,6 ), 1);
	piecesMap["blackKnight0"] = new BlackKnight(board->GetCell( 0,6 ), 0);
	piecesMap["blackKnight1"] = new BlackKnight(board->GetCell( 0,1 ), 1);
	
	piecesMap["whiteBishop0"] = new WhiteBishop(board->GetCell( 7,2 ), 0);
	piecesMap["whiteBishop1"] = new WhiteBishop(board->GetCell( 7,5 ), 1);
	piecesMap["blackBishop0"] = new BlackBishop(board->GetCell( 0,5 ), 0);
	piecesMap["blackBishop1"] = new BlackBishop(board->GetCell( 0,2 ), 1);
	
	piecesMap["whitePawn0"] = new WhitePawn(board->GetCell( 6,0 ), 0);
	piecesMap["whitePawn1"] = new WhitePawn(board->GetCell( 6,1 ), 1);
	piecesMap["whitePawn2"] = new WhitePawn(board->GetCell( 6,2 ), 2);
	piecesMap["whitePawn3"] = new WhitePawn(board->GetCell( 6,3 ), 3);
	piecesMap["whitePawn4"] = new WhitePawn(board->GetCell( 6,4 ), 4);
	piecesMap["whitePawn5"] = new WhitePawn(board->GetCell( 6,5 ), 5);
	piecesMap["whitePawn6"] = new WhitePawn(board->GetCell( 6,6 ), 6);
	piecesMap["whitePawn7"] = new WhitePawn(board->GetCell( 6,7 ), 7);
	piecesMap["blackPawn0"] = new BlackPawn(board->GetCell( 1,7 ), 0);
	piecesMap["blackPawn1"] = new BlackPawn(board->GetCell( 1,6 ), 1);
	piecesMap["blackPawn2"] = new BlackPawn(board->GetCell( 1,5 ), 2);
	piecesMap["blackPawn3"] = new BlackPawn(board->GetCell( 1,4 ), 3);
	piecesMap["blackPawn4"] = new BlackPawn(board->GetCell( 1,3 ), 4);
	piecesMap["blackPawn5"] = new BlackPawn(board->GetCell( 1,2 ), 5);
	piecesMap["blackPawn6"] = new BlackPawn(board->GetCell( 1,1 ), 6);
	piecesMap["blackPawn7"] = new BlackPawn(board->GetCell( 1,0 ), 7);
}

std::string convertToString(int num){
	char result[12];    
	sprintf( result, "%d", num );
	return result;
}
Piece * Pieces::AddPiece(std::string type,std::string color, int row, int column) {
	std::transform(color.begin(), color.end(), color.begin(), ::toupper);
	std::transform(type.begin(), type.end(), type.begin(), ::toupper);
	if(row==-1 && column == -1){
		if(color == "WHITE"){
			if(type == "ROOK")
				return piecesMap["whiteRook"+convertToString(bRookCount)] = new WhiteRook(NULL, wRookCount++);
			else if(type == "BISHOP")
				return piecesMap["whiteBishop"+convertToString(bBishopCount)] = new WhiteBishop(NULL, wBishopCount++);
			else if(type == "KNIGHT")
				return piecesMap["whiteKnight"+convertToString(bKnightCount)] = new WhiteKnight(NULL, wKnightCount++);
			else if(type == "PAWN")
				return piecesMap["whitePawn"+convertToString(bPawnCount)] = new WhitePawn(NULL, wPawnCount++);
			else if(type == "KING")
				return piecesMap["whiteKing"] = new WhiteKing(NULL);
			else if(type == "QUEEN")
				return piecesMap["whiteQueen"] = new WhiteQueen(NULL);
		} else if(color == "BLACK") {
			if(type == "ROOK")
				return piecesMap["blackRook"+convertToString(bRookCount++)]  = new BlackRook(NULL, bRookCount++);
			else if(type == "BISHOP")
				return piecesMap["blackBishop"+convertToString(bBishopCount++)] = new BlackBishop(NULL, bBishopCount++);
			else if(type == "KNIGHT")
				return piecesMap["blackKnight"+convertToString(bKnightCount++)]  = new BlackKnight(NULL, bKnightCount++);
			else if(type == "PAWN")
				return piecesMap["blackPawn"+convertToString(bPawnCount++)]  = new BlackPawn(NULL, bPawnCount++);
			else if(type == "KING")
				return piecesMap["blackKing"] = new BlackKing(NULL);
			else if(type == "QUEEN")
				return piecesMap["blackQueen"]  = new BlackQueen(NULL);
		}
	} else {
		if(color == "WHITE"){
			if(type == "ROOK")
				return piecesMap["whiteRook"+convertToString(bRookCount)] = new WhiteRook(this->board->GetCell( row,column ), wRookCount++);
			else if(type == "BISHOP")
				return piecesMap["whiteBishop"+convertToString(bBishopCount)] = new WhiteBishop(this->board->GetCell( row,column ), wBishopCount++);
			else if(type == "KNIGHT")
				return piecesMap["whiteKnight"+convertToString(bKnightCount)] = new WhiteKnight(this->board->GetCell( row,column ), wKnightCount++);
			else if(type == "PAWN")
				return piecesMap["whitePawn"+convertToString(bPawnCount)] = new WhitePawn(this->board->GetCell( row,column ), wPawnCount++);
			else if(type == "KING")
				return piecesMap["whiteKing"] = new WhiteKing(this->board->GetCell( row,column ));
			else if(type == "QUEEN")
				return piecesMap["whiteQueen"] = new WhiteQueen(this->board->GetCell( row,column ));
		} else if(color == "BLACK") {
			if(type == "ROOK")
				return piecesMap["blackRook"+convertToString(bRookCount++)]  = new BlackRook(this->board->GetCell( row,column ), bRookCount++);
			else if(type == "BISHOP")
				return piecesMap["blackBishop"+convertToString(bBishopCount++)] = new BlackBishop(this->board->GetCell( row,column ), bBishopCount++);
			else if(type == "KNIGHT")
				return piecesMap["blackKnight"+convertToString(bKnightCount++)]  = new BlackKnight(this->board->GetCell( row,column ), bKnightCount++);
			else if(type == "PAWN")
				return piecesMap["blackPawn"+convertToString(bPawnCount++)]  = new BlackPawn(this->board->GetCell( row,column ), bPawnCount++);
			else if(type == "KING")
				return piecesMap["blackKing"] = new BlackKing(this->board->GetCell( row,column ));
			else if(type == "QUEEN")
				return piecesMap["blackQueen"]  = new BlackQueen(this->board->GetCell( row,column ));
		}
	}
	return NULL;
	
}


Piece* Pieces::gWhiteKing() {
	return piecesMap["whiteKing"];
}
Piece* Pieces::gBlackKing() {
	return  piecesMap["blackKing"];
}

Piece* Pieces::gWhiteQueen() {
	return  piecesMap["whiteQueen"];
}
Piece* Pieces::gBlackQueen() {
	return  piecesMap["blackQueen"];
}

Piece* Pieces::gWhiteRook0() {
	return  piecesMap["whiteRook0"];
}
Piece* Pieces::gWhiteRook1() {
	return  piecesMap["whiteRook1"];
}
Piece* Pieces::gBlackRook0() {
	return  piecesMap["blackRook0"];
}
Piece* Pieces::gBlackRook1() {
	return  piecesMap["blackRook1"];
}

Piece* Pieces::gWhiteKnight0() {
	return  piecesMap["whiteKnight0"];
}
Piece* Pieces::gWhiteKnight1() {
	return  piecesMap["whiteKnight1"];
}
Piece* Pieces::gBlackKnight0() {
	return  piecesMap["blackKnight0"];
}
Piece* Pieces::gBlackKnight1() {
	return  piecesMap["blackKnight1"];
}

Piece* Pieces::gWhiteBishop0() {
	return  piecesMap["whiteBishop0"];
}
Piece* Pieces::gWhiteBishop1() {
	return  piecesMap["whiteBishop1"];
}
Piece* Pieces::gBlackBishop0() {
	return  piecesMap["blackBishop0"];
}
Piece* Pieces::gBlackBishop1() {
	return  piecesMap["blackBishop1"];
}

Piece* Pieces::gWhitePawn0() {
	return  piecesMap["whitePawn0"];
}
Piece* Pieces::gWhitePawn1() {
	return  piecesMap["whitePawn1"];
}
Piece* Pieces::gWhitePawn2() {
	return  piecesMap["whitePawn2"];
}
Piece* Pieces::gWhitePawn3() {
	return  piecesMap["whitePawn3"];
}
Piece* Pieces::gWhitePawn4() {
	return  piecesMap["whitePawn4"];
}
Piece* Pieces::gWhitePawn5() {
	return  piecesMap["whitePawn5"];
}
Piece* Pieces::gWhitePawn6() {
	return  piecesMap["whitePawn6"];
}
Piece* Pieces::gWhitePawn7() {
	return  piecesMap["whitePawn7"];
}

Piece* Pieces::gBlackPawn0() {
	return  piecesMap["blackPawn0"];
}
Piece* Pieces::gBlackPawn1() {
	return  piecesMap["blackPawn1"];
}
Piece* Pieces::gBlackPawn2() {
	return  piecesMap["blackPawn2"];
}
Piece* Pieces::gBlackPawn3() {
	return  piecesMap["blackPawn3"];
}
Piece* Pieces::gBlackPawn4() {
	return  piecesMap["blackPawn4"];
}
Piece* Pieces::gBlackPawn5() {
	return  piecesMap["blackPawn5"];
}
Piece* Pieces::gBlackPawn6() {
	return  piecesMap["blackPawn6"];
}
Piece* Pieces::gBlackPawn7() {
	return  piecesMap["blackPawn7"];
}



